package com.ucenter.server.auth.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.system.comm.model.KvEntity;
import com.system.comm.model.Page;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;
import com.ucenter.server.auth.dao.ResInfoDao;
import com.ucenter.server.auth.enums.ResInfoType;
import com.ucenter.server.auth.pojo.ResInfo;
import com.ucenter.server.auth.service.ResInfoService;

/**
 * res_info的Service
 * @author autoCode
 * @date 2018-12-01 09:50:44
 * @version V1.0.0
 */
@Component
public class ResInfoServiceImpl implements ResInfoService {

	@Autowired
	private ResInfoDao resInfoDao;
	
	@Override
	public ResponseFrame saveOrUpdate(ResInfo resInfo) {
		ResponseFrame frame = new ResponseFrame();
		ResInfo org = get(resInfo.getResId());
		if (resInfo.getLevel() == null) {
			Integer level = 1;
			if (!"0".equals(resInfo.getParentResId())) {
				String levelResId = resInfo.getParentResId();
				for (int i = 0; i < 10; i++) {
					ResInfo levelRes = get(levelResId);
					level ++;
					if ("0".equals(levelRes.getParentResId())) {
						break;
					}
					levelResId = levelRes.getParentResId();
				}
			}
			resInfo.setLevel(level);
		}
		if(org == null) {
			resInfoDao.save(resInfo);
		} else {
			resInfoDao.update(resInfo);
		}
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public ResInfo get(String resId) {
		return resInfoDao.get(resId);
	}

	@Override
	public ResponseFrame pageQuery(ResInfo resInfo) {
		resInfo.setDefPageSize();
		ResponseFrame frame = new ResponseFrame();
		int total = resInfoDao.findResInfoCount(resInfo);
		List<ResInfo> data = null;
		if(total > 0) {
			data = resInfoDao.findResInfo(resInfo);
			for (ResInfo res : data) {
				res.setTypeName(ResInfoType.getText(res.getType()));
				handleTipName(res);
			}
		}
		Page<ResInfo> page = new Page<ResInfo>(resInfo.getPage(), resInfo.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	private void handleTipName(ResInfo res) {
		if (res.getLevel() == 1) {
			res.setTipName(res.getShowName());
		} else if (res.getLevel() == 2) {
			res.setTipName("|-- " + res.getShowName());
		} else if (res.getLevel() == 3) {
			res.setTipName("|---- " + res.getShowName());
		} else if (res.getLevel() == 4) {
			res.setTipName("|------ " + res.getShowName());
		} else if (res.getLevel() == 5) {
			res.setTipName("|-------- " + res.getShowName());
		} else {
			res.setTipName(res.getShowName());
		}
	}
	
	@Override
	public ResponseFrame delete(String resId) {
		ResponseFrame frame = new ResponseFrame();
		resInfoDao.delete(resId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public List<KvEntity> findKvAll() {
		List<ResInfo> resList = resInfoDao.findAll();
		List<KvEntity> data = new ArrayList<KvEntity>();
		for (ResInfo res : resList) {
			handleTipName(res);
			data.add(new KvEntity(res.getResId(), res.getTipName()));
		}
		return data;
	}

	@Override
	public List<KvEntity> findKvByWebsiteId(String websiteId) {
		List<ResInfo> resList = findByWebsiteId(websiteId);
		List<KvEntity> data = new ArrayList<KvEntity>();
		for (ResInfo res : resList) {
			data.add(new KvEntity(res.getResId(), res.getTipName()));
		}
		return data;
	}

	@Override
	public List<ResInfo> findByWebsiteId(String websiteId) {
		List<ResInfo> list = resInfoDao.findByWebsiteId(websiteId);
		for (ResInfo res : list) {
			handleTipName(res);
		}
		return list;
	}
}